package com.mvp.shq.washer.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ShopSpecAdapter;
import com.mvp.shq.washer.entity.shop.ShopSpec;

import java.util.List;

/**
 * Created by shq1 on 2017/10/9.
 */

public class ShopSelectMenu extends PopupWindow {

    private View mPopView;
    private Activity activity;
    private Button btnReset,btnConfirm;
    private RecyclerView recyclerView;
    private List<ShopSpec> data;
    private ShopSpecAdapter adapter;
    private OnConfirmClickListener listener;

    public ShopSelectMenu(Context context, Activity mActivity, List<ShopSpec> list, OnConfirmClickListener clickListener) {
        super(context);
        activity = mActivity;
        data = list;
        listener = clickListener;
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.ranking_menu, null);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });
        btnReset = (Button)mPopView.findViewById(R.id.btn_reset);
        btnConfirm = (Button)mPopView.findViewById(R.id.btn_confirm);
        recyclerView = (RecyclerView)mPopView.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ShopSpecAdapter(context, data, new ShopSpecAdapter.OnAttrClickListener() {
            @Override
            public void onClick(int specPosition, int attrPosition) {
                boolean select = data.get(specPosition).getAttrs().get(attrPosition).isSelect();
                //data.get(specPosition).getAttrs().get(attrPosition).setSelect(!select);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i=0;i<data.size();i++){
                    for (int j=0;j<data.get(i).getAttrs().size();j++){
                        data.get(i).getAttrs().get(j).setSelect(false);
                    }
                }
                adapter.reset();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = "";
                for (int i=0;i<data.size();i++){
                    for (int j=0;j<data.get(i).getAttrs().size();j++){
                        if (data.get(i).getAttrs().get(j).isSelect()){
                            str += data.get(i).getAttrs().get(j).getId();
                            str += ",";
                        }
                    }
                }
                listener.onClick(str);
                dismiss();
            }
        });
        adapter.setMultiselect(true);
        recyclerView.setAdapter(adapter);
    }

    public interface OnConfirmClickListener{
        void onClick(String content);
    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.popwindow_anim_top);// 设置动画
        this.setBackgroundDrawable(new BitmapDrawable());// 设置背景透明
        //this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
